package com.nxy.cpfsfe.service.factory;

import com.adtec.starring.exception.BaseException;
import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 借记卡信息校验申请处理
 * Created by wyj on 2019/12/25.
 */
public class DebitCrdInfChkServer implements ForwardServer{
    private static DebitCrdInfChkServer server = new DebitCrdInfChkServer();
    private DebitCrdInfChkServer(){}
    public static DebitCrdInfChkServer getInstance(){
        return server;
    }
    private final static Logger LOGGER = LoggerFactory.getLogger(DebitCrdInfChkServer.class);

    @Override
    public String buildEsbXml(Map<String, String> map) {
        LOGGER.info("---------借记卡信息校验申请报文CBS0000020--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE, SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CBS" + SysDef.FILE_SP + "CBS_0000020.xml");
            //报文头
            XmlCBSBuildUtil.buildXmlRequestHead(xmlReqDoc, MsgCdOrTpEnum.CBS_0000020.getCode(), map);
            //报文体
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cardAcctNb ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cardAcctFlg",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctNoFlg")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/pwdTyp",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctPwdTp")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/pwd",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctPwd")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/chkCertFlg",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/CertChkFlg")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/certTyp", StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/CertTp")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/certNb",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/CertNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/custNm",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctNm")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cvv",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/CVV")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("借记卡信息校验申请报文CBS0000020失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "借记卡信息校验申请报文CBS0000020失败");
        }
    }

    @Override
    public String handleRpsMsg(Map<String, String> map, String respMsg) {
        LOGGER.info("---------借记卡信息校验回执报文CBS0000021--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CPFS" + SysDef.FILE_SP + "CPFS_303.xml");
            //报文头
            XmlCPFSBuildUtil.buildXmlResponseHead(xmlReqDoc, MsgCdOrTpEnum.MSGTD_303.getCode(), map);
            //解析cbs0000021报文
            if(StringUtils.isNotBlank(respMsg)){
                Map<String,String> rpsmap = XmlUtil.xml2Map(respMsg);
                map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                map.put("resultCode",StringUtil.trimObject(rpsmap.get("/transaction/header/status/retCd")));
            }else{
                LOGGER.error("cbs0000021回执报文为空，请联系");
                map.put("msg", "cbs0000021回执报文为空，请联系");
                map.put("resultCode",RespCd.NCPO0005);
            }
            //报文体
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsDesc",  StringUtil.trimObject(map.get("msg")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("借记卡信息校验回执报文CBS0000021失败", e);

            throw new BaseException(RespCd.NCPS2402, e, "借记卡信息校验回执报文CBS0000021失败");
        }
    }
}
